/**
 *------------------------------------------------------------------------------
 *  @copyright Copyright (c) 2022  <dx_65535@163.com>. 
 * 
 *  @file       : ipc_parse.cc
 *  @author     : Xiang.D (dx_65535@163.com)
 *  @version    : 1.0
 *  @brief      : Blog: https://linuxtaoist.gitee.io
 *  @date       : 2022-06-04
 * 
 * 
 *  Change History:
 *  <Date>     | <Version> | <Author>       | <Description>
 *------------------------------------------------------------------------------
 *  2022/06/04 | 1.0.0.1   | Xiang.D        | Create file
 *------------------------------------------------------------------------------
 * 
 */

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <mqueue.h>
#include "mq_parse.h"

#define LOGE(fmt, args...) printf("%d IpcParse E: " fmt, __LINE__, ##args)

#ifdef TEST_DEBUG
#define LOGD(fmt, args...) printf("%d IpcParse D: " fmt, __LINE__, ##args)
#else
#define LOGD(fmt, args...)
#endif

CMqParse::CMqParse()
{
}

CMqParse* CMqParse::GetInstance()
{
    static CMqParse instance;
    return &instance;
}

int CMqParse::ParseFrame(int fd, SCmdFrame *pFrame)
{
    ssize_t ret = 0;
    unsigned int prio = 0;
    mq_attr mqAttr;

    mq_getattr(fd, &mqAttr);
    LOGD("-> Ipc data parse.\n");

    // read data from fd
    ret = mq_receive(fd, (char *)pFrame, mqAttr.mq_msgsize, &prio);
    if (ret < 0) {
        LOGE("read data failed. (%s)\n", strerror(errno));
    } else {
        LOGD("read data size: %ld cmd: 0x%x.\n", ret, pFrame->channel);
    }

    return 0;
}

